﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Company_ConfirmBidPassword : System.Web.UI.Page, IAccess
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        // 如果已经设置密码

        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(CurrentAccount.Company_Project_Switch.BidPassword))
            {
                HZ.Web.MessageBox.Show("已经设置报价密码，无须再设置！！");
            }

            HZ.BidSystem.BLL.BLL_Cache.SetOnlineModel(CurrentAccount.Account.AccountId.Value, new HZ.BidSystem.Model.OnLineModel() { Active = "正在设置报价密码" });
        }

    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        //Page.Validate();
        //if (Page.IsValid)
        //{

        if (!string.IsNullOrEmpty(CurrentAccount.Company_Project_Switch.BidPassword))
        {
            HZ.Web.MessageBox.Show("已经设置报价密码，无须再设置！！");
            return;
        }

        // 检查长度
        string pwd1 = txtBidPwd1.Text.Trim().Replace(" ","");
        string pwd2 = txtBidPwd2.Text.Trim().Replace(" ", "");

        if (pwd1 != pwd2)
        {
            HZ.Web.MessageBox.Show("您两次输入的报价密码不一致");
            return;
        }

        if (pwd1.Length < 6)
        {
            HZ.Web.MessageBox.Show("您密码长度小于6位，建议您采用长度高于6位的字符作为密码");
            return;
        }
        // string randpwd = Util.GetRandomStr(6);
        string randpwd = CurrentAccount.Account.AccountName.ToUpper();
        if (BLL_Factory.IBLL_Company.SetBidPwd(pwd1, randpwd, CurrentAccount.Account.AccountId.Value, CurrentAccount.ProjectId) != 1)
        {
            HZ.Web.MessageBox.Show("设置密码失败，请联系软件工程师支持。");
            return;
        }

        HZ.Common.log4netManager.LogInfo("设置报价密码", string.Format("设置报价密码成功:设置报价密码用户{0},登录IP地址{1}", CurrentAccount.Account.AccountName, HZ.Fun.GetClientIP()), null);

        lblpwd.Text = pwd1;
        // lblPwdRandom.Text = randpwd;

        //使用Session记录 密码
        CurrentAccount.BidPwd = pwd1;
        CurrentAccount.RandPwd = randpwd;


        panInfo.Visible = true;
        panConfirmPwd.Visible = false;
        //}
        //else
        //{
        //    HZ.Web.WebUtil.Alert("客户端验证失败", this);
        //}

    }
    protected void lklSave_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine(string.Format("您刚才输入的报价密码为：{0} 。", Session["BidPwd"]));
        sb.AppendLine("请牢牢记住该密码，并注意大小写，否则将无法报价和解密。");

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";

        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=UserPassWord.txt");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "text/plain";
        Response.Write(sb.ToString());
        Response.End();

    }

    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Company);
            return list;
        }

    }

    #endregion
}
